package com.xiaoxu.privatechat.service.home;

import com.xiaoxu.privatechat.dao.home.FriendRequestDao;
import com.xiaoxu.privatechat.entity.common.Account;
import com.xiaoxu.privatechat.entity.home.FriendRequest;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author xiaoxu
 * @description 好友请求service
 * @date 2023/2/13
 */
@Service
public class FriendRequestService {

    @Resource
    private FriendRequestDao friendRequestDao;

    /**
     * @param friendRequest 好友请求
     * @return FriendRequest
     * @description 更新或新增好友请求
     */
    public FriendRequest save(FriendRequest friendRequest) {
        return friendRequestDao.save(friendRequest);
    }

    /**
     * @param sender   发送方
     * @param reciever 接收方
     * @return FriendRequest
     * @description 根据发出者、接受者查询
     */
    public FriendRequest findBySenderAndReciever(Account sender, Account reciever) {
        return friendRequestDao.findBySenderAndReciever(sender, reciever);
    }

    /**
     * @param id 好友请求id
     * @return FriendRequest
     * @description 根据id查询
     */
    public FriendRequest find(Long id) {
        return friendRequestDao.find(id);
    }

    /**
     * @param reciever 接收者
     * @return List<FriendRequest>
     * @description 根据接受者查询
     */
    public List<FriendRequest> findByReciever(Account reciever) {
        return friendRequestDao.findByRecieverOrderByUpdateTimeDesc(reciever);
    }

    /**
     * @param id 好友请求id
     * @description 删除好友请求
     */
    public void delete(Long id) {
        friendRequestDao.deleteById(id);
    }

    /**
     * @param recieverId 接收者id
     * @return Integer
     * @description 获取新的好友添加请求数
     */
    public Integer getNewFriendRequestCount(Long recieverId) {
        return friendRequestDao.getFriendRequestCount(recieverId, FriendRequest.FRIEND_REQUEST_STATUS_WAITING);
    }
}
